<?php
if (! class_exists ( "stmodel_1_0" )) {
	class stmodel_1_0 {
		protected static $_instance = null;
		protected $wpdb = null;
		public $config = null;

                protected $_tableName = "";
                protected $_primaryKey = "";
                public $option = '';

		public function __construct($config) {
			global $wpdb;
			$this->wpdb = $wpdb;
                        $this->config = $config;
                        $this->config ['pluginDir'] = WP_PLUGIN_DIR . '/' . $config ['plugin_name'] . '/';
                        $this->option = get_option($this->config['plugin_name'].'_'.$this->config['option_key'].'_settings');
		}

                public function loadmodel($modelName) {
			$className = $modelName . "_model";
			$fileName = $this->config ['pluginDir'] . "models/{$modelName}.php";
			require_once $fileName;
			$obj = new $className($this->config);
			return $obj;
		}

		public static function getInstance() {
			if (null === self::$_instance) {
				self::$_instance = new self ();
			}

			return self::$_instance;
		}

		public static function getTable($tableName) {
			$table = new self ();
			$table->setTableName ( $tableName );
			return $table;
		}

		public function setTableName($tableName) {
			$this->_tableName = $tableName;
		}

                public function setPrimaryKey($primaryKey) {
			$this->_primaryKey = $primaryKey;
		}

		public function __call($function, $args) {
                    return call_user_func_array(
                        array($this->wpdb, $function),
                        $args
                    );
		}

		public function __get($key) {
			return $this->wpdb->$key;
		}

                public function getRecords($args = ''){
                    $default_args = array(
                        'fields' => '*',
                        'and_where' => '',
                        'or_where' => '',
                        'custom_where' => '',
                        'order' => '',
                        'limit' => '',
                        'group_by' => '',
                        'custom_query' => '',
                    );
                    if($args == '')
                        $args = $default_args;

                    $fields = $args['fields']? $args['fields']:'*';
                    $and_where = $args['and_where']? $args['and_where']:'';
                    $or_where = $args['or_where']? $args['or_where']:'';
                    $custom_where = $args['custom_where']? $args['custom_where']:'';
                    $order = $args['order']? $args['order']:'';
                    $limit = $args['limit']? $args['limit']:'';
                    $group_by = $args['group_by']? $args['group_by']:'';
                    $custom_query = $args['custom_query']? $args['custom_query']:'';
                    if( !empty($custom_query)){
                        $query = $custom_query;
                    }else{
                        if($fields != '*')
                            $fields = implode(', ', $fields);
                        //
                        $where = ' WHERE ';
                        if($and_where != '')
                            $where .= implode (' AND ', $and_where);
                        if($or_where != '')
                            $where .= implode (' OR ', $or_where);
                        if($custom_where != '')
                            $where = $custom_where;
                        if($where == ' WHERE ')
                            $where = '';
                        //
                        if($order != '')
                            $order = ' ORDER BY '. implode(' ', $order);
                        //
                        if($limit != '')
                            $limit = ' LIMIT '.implode (', ', $limit);
                        if($group_by != '')
                            $group_by = ' GROUP BY ' . $group_by;
                        //
                        $query = "SELECT $fields
                        FROM $this->_tableName
                        $where
                        $group_by
                        $order
                        $limit
                        ";
                    }
                    //echo $query;
                    return $this->get_results($query);
                }

                public function deleteRecords($id = ''){
                    $query = "DELETE FROM $this->_tableName
                    WHERE `{$this->_primaryKey}` = $id";
                    return $this->query($query);
                }

                public function saveRecords($data = '', $where = ''){
                    if($data[$this->_primaryKey]){
                        if($where == ''){
                            $where = array($this->_primaryKey => intval($data[$this->_primaryKey]));
                        }
                        return $this->update($this->_tableName, $data, $where);
                    } else {
                        return $this->insert($this->_tableName, $data);                        
                    }
                }

                public function countRecords(){
                    $query = "SELECT COUNT(*)
                    FROM $this->_tableName
                    ";
                    $result = $this->get_var($query);
                    return $result;
                }

        }
}
?>